<?php
/**
 * 佣金设置
 */
namespace Admin\Controller;
use Think\Controller;
class CommissionController extends InitController {
    
    public function Index(){
    	$topmenu = $this->topmenu;
		$sidebarmenu = $this->sidebarmenu[I('get.k')];
    	

    	$rule = M('CommissionRule')->where(1)->field('level_1_brokerage,level_2_brokerage,level_3_brokerage,agency')->find();
		
		//用于下面的更新的判断
		$result = $rule;
		
    	$rule['level_1_brokerage'] += 0;
    	$rule['level_2_brokerage'] += 0;
    	$rule['level_3_brokerage'] += 0;
		$rule['agency']            += 0;
	
    	if(IS_AJAX){
    		$level_1_brokerage = I('level_1_brokerage') + 0;
	    	$level_2_brokerage = I('level_2_brokerage') + 0;
	    	$level_3_brokerage = I('level_3_brokerage') + 0;
			$agency            = I('agency')+ 0;
	    	if(($level_1_brokerage+$level_2_brokerage+$level_3_brokerage + $agency) > 100){
	    		$this->ajaxReturn(array('error'=>1,'errmsg'=>'三级佣金和总代佣金相加不能大于100','field'=>''));
	    	}
	    	$add = array(
	    		'level_1_brokerage'=>$level_1_brokerage,
	    		'level_2_brokerage'=>$level_2_brokerage,
	    		'level_3_brokerage'=>$level_3_brokerage,
				'agency'=>$agency
	    	);
    		if($result){
    			$ac = M('CommissionRule')->where(1)->save($add);
    		}else{
    			$add['createtime'] = NOW_TIME;
    			$ac = M('CommissionRule')->add($add);
    		}

    		if($ac){
    			$this->ajaxReturn(array('error'=>0,'errmsg'=>'操作成功！','field'=>''));
    		}else{
    			$this->ajaxReturn(array('error'=>1,'errmsg'=>'操作失败！','field'=>''));
    		}
    	}
    	


    	$this->assign(array(
    		'topmenu'=>$topmenu,
    		'sidebarmenu'=>$sidebarmenu,
    		'rule'=>$rule,	
    	));
    	$this->display('Index');
    }
	
}